//
//  BEMessageController.swift
//  BEGlobalOiler
//
//  Created by KKING on 16/10/1.
//  Copyright © 2016年 Bullseye Energy pte ltd. All rights reserved.
//

import UIKit
import IQKeyboardManagerSwift

class BEMessageController: RCConversationListViewController {
    
    override class func initialize() {
        self.init().setConversationAvatarStyle(.USER_AVATAR_CYCLE)
        self.init().cellBackgroundColor = COLOR_NAV_BG
    }
    override func viewDidLoad() {
        //重写显示相关的接口，必须先调用super，否则会屏蔽SDK默认的处理
        super.viewDidLoad()
        
        navigationItem.leftBarButtonItem = UIBarButtonItem.leftItem(normalImage: "tabbar_contact", highlightedImage: "tabbar_contact", target: self, action: #selector(leftBtnClick))

         navigationItem.rightBarButtonItem = UIBarButtonItem.rightItem(normalImage: "tabbar_message", highlightedImage: "tabbar_message", target: self, action: #selector(rightBtnClick))
        
        
        RCIM.shared().connect(withToken: "pX3wr6UpZLNCkI1rxEU6e1g7HG9Lp/oEaPeguBlLJTWoEiqz/Fa1fyg2lNrfA49thsomYnrbQw++dPmILzpNEA==",
                                           success: { (userId) -> Void in

            }, error: { (status) -> Void in
                //                print("登陆的错误码为:\(status.rawValue)")
            }, tokenIncorrect: {
                //token过期或者不正确。
                //如果设置了token有效期并且token过期，请重新请求您的服务器获取新的token
                //如果没有设置token有效期却提示token错误，请检查您客户端和服务器的appkey是否匹配，还有检查您获取token的流程。
                //                print("token错误")
        })
        
        //设置需要显示哪些类型的会话
        self.setDisplayConversationTypes([RCConversationType.ConversationType_PRIVATE.rawValue,
            RCConversationType.ConversationType_DISCUSSION.rawValue,
            RCConversationType.ConversationType_CHATROOM.rawValue,
            RCConversationType.ConversationType_GROUP.rawValue,
            RCConversationType.ConversationType_APPSERVICE.rawValue,
            RCConversationType.ConversationType_SYSTEM.rawValue])
        //设置需要将哪些类型的会话在会话列表中聚合显示
        self.setCollectionConversationType([RCConversationType.ConversationType_DISCUSSION.rawValue,
            RCConversationType.ConversationType_GROUP.rawValue])
    }
    
    @objc func leftBtnClick(){
        view.backgroundColor = UIColor.init(red: 244/255.0, green: 245/255.0, blue: 249/255.0, alpha: 1.0)
        let phone = UserDefaults.standard.object(forKey: "phone") as? String
        
        let alertVC = UIAlertController(title: "CALL Admin?", message: phone, preferredStyle: .alert)
        
        let cancleAction = UIAlertAction(title: "Cancle", style: .cancel, handler: nil)
        let doneAction = UIAlertAction(title: "Sure", style: .default) { (_) in
            
            let url = NSURL(string: "tel://\(phone)")
            UIApplication.shared.openURL(url! as URL)
        }
        
        alertVC.addAction(cancleAction)
        alertVC.addAction(doneAction)
        
        
        UIApplication.shared.keyWindow?.rootViewController!.present(alertVC, animated: true, completion: nil)
    }
    
    
    @objc func rightBtnClick(){
        //新建一个聊天会话View Controller对象
        let chat = RCConversationViewController()
        //设置会话的类型，如单聊、讨论组、群聊、聊天室、客服、公众服务会话等
        chat.conversationType = RCConversationType.ConversationType_PRIVATE
        //设置会话的目标会话ID。（单聊、客服、公众服务会话为对方的ID，讨论组、群聊、聊天室为会话的ID）
        chat.targetId = "targetIdYouWillChatIn"
        //设置聊天会话界面要显示的标题
        chat.title = "Customer service"
        //显示聊天会话界面
        self.navigationController?.pushViewController(chat, animated: true)
    }

    //重写RCConversationListViewController的onSelectedTableRow事件
    override func onSelectedTableRow(_ conversationModelType: RCConversationModelType, conversationModel model: RCConversationModel!, at indexPath: IndexPath!) {
        //打开会话界面
        let chat = RCConversationViewController(conversationType: model.conversationType, targetId: model.targetId)
        //        chat.title = "想显示的会话标题"
        self.navigationController?.pushViewController(chat!, animated: true)
    }    
}
